package com.zhihuichongcheng.application.ui.cricle.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.zhihuichongcheng.application.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/31.
 */

public class AddImageAdapter extends RecyclerView.Adapter<AddImageAdapter.ViewHold> {
    private Context mContext;
    private View.OnClickListener onClickListener;
    private List<String> mList = new ArrayList<>();
    private int width;
    public AddImageAdapter(Context context)
    {
        this.mContext = context;
         width = DeviceUtil.getWidth(mContext);
         width = width-DeviceUtil.dip2px(mContext,20);
    }
    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_add_image,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width/4-DeviceUtil.dip2px(mContext,10),width/4-DeviceUtil.dip2px(mContext,10));
        params.setMargins(DeviceUtil.dip2px(mContext,5),DeviceUtil.dip2px(mContext,0),DeviceUtil.dip2px(mContext,5),DeviceUtil.dip2px(mContext,15));
        holder.mImAdd.setLayoutParams(params);
        if(position<=8)
        {
            Glide.with(mContext).load(mList.get(position)).placeholder(R.drawable.ic_cricle_add_img).into(holder.mImAdd);
        }

        holder.mImAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if((position == mList.size()-1)&&TextUtils.isEmpty(mList.get(position))&&mList.size()<=9)
                {
                    if(onClickListener!=null)
                    {
                        onClickListener.onClick(v);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public List<String> getData() {
        return mList;
    }

    public void setData(String str)
    {
        if(mList.size()>0)
        {
            this.mList.add(mList.size()-1,str);
        }else if(mList.size()==9)
        {
            this.mList.set(mList.size()-1,str);
        }
        else
        {
            this.mList.add(str);
        }

    }

    class ViewHold extends RecyclerView.ViewHolder {
        private ImageView mImAdd;

        public ViewHold(View itemView) {
            super(itemView);
            mImAdd = (ImageView) itemView.findViewById(R.id.im_image);
        }
    }
}
